package com.snake.springboot.custom.service.impl.login;


import com.snake.springboot.custom.api.model.base.CustomAccountModel;
import com.snake.springboot.custom.api.model.common.AccountType;
import com.snake.springboot.custom.api.model.common.CommonVal;
import com.snake.springboot.custom.api.model.login.AttachedObject;
import com.snake.springboot.custom.api.model.login.LoginModel;
import com.snake.springboot.custom.api.response.login.MainAccLoginRsp;
import com.snake.springboot.custom.api.service.login.MainAccLoginService;
import com.snake.springboot.custom.dao.mybatis.entity.base.CustMainAccountDO;
import com.snake.springboot.custom.dao.mybatis.mappers.base.CustMainAccountMapper;
import com.snake.springboot.custom.service.assemble.login.CustomAccLoginAssembler;
import com.snake.springboot.custom.service.port.adapter.event.AuthDelAccCacheEvt;
import com.snake.springboot.custom.service.port.adapter.event.VerifyTextCodeEvent;
import org.snake.common.exception.LoginException;
import org.snake.common.security.MD5Util;
import org.snake.common.security.account.AccountData;
import org.snake.common.security.jwt.DefaultProperties;
import org.snake.common.security.jwt.JwtHelper;
import org.snake.common.utils.PropertyUtils;
import org.snake.redis.commons.RedisPlusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>主账号登录serviceImpl
 * 类{@code MainAccLoginServiceImpl} </br>
 * 创建时间2025/2/13 14:19
 * @author yangxt
 * @see
 */
@Service
public class MainAccLoginServiceImpl implements MainAccLoginService {
    private static String CustomLoginRedisKey = "Custom:LoginRedisKey:%s";
    @Autowired
    private ApplicationContext aCt;
    @Autowired
    private CustMainAccountMapper accountMapper;
    @Autowired
    private RedisPlusUtils redisPlusUtils;
    @Autowired
    private DefaultProperties defaultProperties;

    @Resource
    private JwtHelper custJwtHelper;

    @Override
    public MainAccLoginRsp login(LoginModel model) {
        //验证码校验
        //VerifyImageEvent event = new VerifyImageEvent(this, model.getVerifyImageModel());
        //文字验证码校验
        VerifyTextCodeEvent event = new VerifyTextCodeEvent(this, model.getVerifyTextCodeModel());
        aCt.publishEvent(event);
        CustMainAccountDO accountDO = accountMapper.loginAccount(model.getAccount(),MD5Util.encrypt(model.getPassword()));
        if (accountDO == null) {
            throw new LoginException("账号或者密码错误");
        }
        if (!CommonVal.Y.equals(accountDO.getEnable())){
            throw new LoginException("账号不可用");
        }
        String token = custJwtHelper.sign(accountDO.getCustomId(), AccountType.MAIN_ACC, MD5Util.encrypt(accountDO.getPassword()+accountDO.getCustomId()));
        //删除缓存数据
        AuthDelAccCacheEvt evt = new AuthDelAccCacheEvt(this, accountDO.getCustomId());
        aCt.publishEvent(evt);
        MainAccLoginRsp rsp = new MainAccLoginRsp();
        PropertyUtils.copyProperties(rsp,accountDO);
        rsp.setToken(token);
        rsp.setAccountId(accountDO.getMainAccountId());
        rsp.setAccountType(AccountType.MAIN_ACC);

        //加入redis缓存
        CustomAccountModel customAccountModel = CustomAccLoginAssembler.assembleCustomAccount(rsp);
        redisPlusUtils.setOpsVal(String.format(CustomLoginRedisKey,token),customAccountModel,
                defaultProperties.getMaxAliveSecond());
        return rsp;
    }

    @Override
    public AccountData mainAccount(AccountData model) {
        CustMainAccountDO accountDO = accountMapper.queryByCustomId(model.getAccountId());
        if (accountDO == null) {
            return null;
        }
        model.setAccount(accountDO.getAccountName());
        model.setSecret(MD5Util.encrypt(accountDO.getPassword()+accountDO.getCustomId()));
        model.setAttachedObject(new AttachedObject(accountDO.getCustomId()));
        return model;
    }
}
